/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.app.model.AcctRole;
import com.xw.app.model.SysMenu;
import com.xw.app.service.SysMenuService;
import com.xw.app.vo.MenuSelection;
import com.xw.framework.base.controller.BaseSpringController;
import com.xw.framework.vo.UserMenu;
import com.xw.framework.vo.acetree.AceTreeItem;
import com.xw.framework.vo.acetree.AdditionalParameters;
import com.xw.util.BeanUtils;

/**
 * The Class MenuPopupController.
 *
 * @author Xi Wei
 */
@Controller
@RequestMapping("/menupopup")
public class MenuPopupController extends BaseSpringController {

	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * List.
	 *
	 * @return the model and view
	 */
	@RequestMapping("/list")
	public ModelAndView list(){
		ModelAndView result = new ModelAndView("/SysMenu/list");
		ModelMap map = new ModelMap();
		List<SysMenu> menuList = sysMenuService.findByParentIsNull();
		map.addAttribute("menuList", menuList);
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Creates the.
	 *
	 * @param sysMenu the sys menu
	 * @return the model and view
	 */
	@RequestMapping("/create")
	public ModelAndView create(SysMenu sysMenu){
		ModelAndView result = new ModelAndView("/SysMenu/add");
		ModelMap map = new ModelMap();
		if(sysMenu.getId() != null){
			sysMenu = sysMenuService.getById(sysMenu.getId());
		}
		map.addAttribute("sysMenu", sysMenu);
		List<UserMenu> menuList = sysMenuService.getAllMenuCanHaveSubMenu();
		map.addAttribute("menuList", menuList);
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Save.
	 *
	 * @param sysMenu the sys menu
	 * @param validateResult the validate result
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping("/save")
	public ModelAndView save(@Valid SysMenu sysMenu,BindingResult validateResult,HttpServletRequest request){
		if(validateResult.hasErrors()){
			return create(sysMenu);
		}
		int menuLevel = 1;
		if(sysMenu.getParent() != null && sysMenu.getParent().getId() != null){
			sysMenu.setParent(sysMenuService.getById(sysMenu.getParent().getId()));
			menuLevel = sysMenu.getParent().getMenuLevel() + 1;
		}else{
			sysMenu.setParent(null);
		}
		
		if(sysMenu.getId() != null){
			SysMenu existing = sysMenuService.getById(sysMenu.getId());
			sysMenu.setSubMenuList(existing.getSubMenuList());
			BeanUtils.copyProperties(existing, sysMenu);
		}
		sysMenu.setMenuLevel(menuLevel);
		sysMenuService.save(sysMenu);
		ModelMap map = new ModelMap();
		ModelAndView result = new ModelAndView("/saveResult");
		map.addAttribute("msg", "success");
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the string
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Long id){
		sysMenuService.delete(id);
		return "success";
		
	}
	
	/**
	 * To assign menu.
	 *
	 * @param id the id
	 * @return the model and view
	 */
	@RequestMapping("/toAssignMenu")
	public ModelAndView toAssignMenu(Long id)
	{
		ModelAndView result = new ModelAndView("/SysMenu/roleMenuZTree");
		ModelMap map = new ModelMap();

		map.addAttribute("roleId", id);
		result.addAllObjects(map);
		
		return result;
	}
	
	
	/**
	 * Gets the menu tree.
	 *
	 * @return the menu tree
	 */
	@RequestMapping(value={"/getMenuTree","/json/getMenuTree"})
	@ResponseBody
	public Map<String,AceTreeItem> getMenuTree(){
		
		Map<String,AceTreeItem> aceTreeItemMap = new HashMap<String,AceTreeItem>();
		AceTreeItem aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("For Sale");
		aceTreeItem.setType("folder");
		aceTreeItemMap.put("For Sale", aceTreeItem);
		
		AdditionalParameters additionalParameters = new AdditionalParameters();
		additionalParameters.setId(1l);
		
		aceTreeItem.setAdditionalParameters(additionalParameters);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Appliances");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Clothing");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Sports");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		
		
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Vehicles");
		aceTreeItem.setType("folder");
		aceTreeItemMap.put("Vehicle", aceTreeItem);
		
		additionalParameters = new AdditionalParameters();
		additionalParameters.setId(2l);
		aceTreeItem.setAdditionalParameters(additionalParameters);
		
		AceTreeItem carItem = new AceTreeItem();
		carItem.setName("Cars");
		carItem.setType("folder");		
		additionalParameters.addAceTreeItem(carItem);
		
		
		additionalParameters = new AdditionalParameters();
		additionalParameters.setId(3l);
		carItem.setAdditionalParameters(additionalParameters);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Classic");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		aceTreeItem = new AceTreeItem();
		aceTreeItem.setName("Coupes");
		aceTreeItem.setType("item");
		additionalParameters.addAceTreeItem(aceTreeItem);
		
		return aceTreeItemMap;
		
		
		
		
	}
	
}
